package com.lianwei.hospital.controller;


import com.lianwei.hospital.Service.Impl.OutInfoImpl;
import com.lianwei.hospital.Service.OutInfoService;
import com.lianwei.hospital.module.entity.OutInfo;
import com.lianwei.hospital.module.vo.OutVO;
import com.lianwei.hospital.utils.PageUtil;
import com.lianwei.hospital.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/out")
public class OutInfoController {
    @Autowired
    private OutInfoService outInfoService;

    @GetMapping("/selectById")
    public Result<OutInfo> Select(int i) throws Exception {
        System.out.println(i+"\n\n");
        OutInfo outInfo = outInfoService.selectByPrimaryKey(i);
        return Result.buildResult(Result.Status.OK,outInfo);
    }

    @PostMapping
    public Result<List<OutInfo>> Insert(@RequestBody OutInfo outInfo) throws Exception {
        System.out.println("增加时"+outInfo);
        if(outInfoService.insert(outInfo)){
            List<OutInfo> outInfoList = outInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,outInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @PostMapping("deleteById")
    public Result<List<OutInfo>> Delete(@RequestBody OutInfo outInfo) throws Exception {
        System.out.println("删除时"+outInfo);
        if(outInfoService.deleteByPrimaryKey(outInfo.getId())){
            List<OutInfo> outInfoList = outInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,outInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @PutMapping
    public Result<List<OutInfo>> Update(@RequestBody OutInfo outInfo) throws Exception {
        System.out.println("更新时"+outInfo);
        if(outInfoService.updateByPrimaryKey(outInfo)){
            List<OutInfo> outInfoList = outInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,outInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @GetMapping("/selectAll")
    public Result<PageUtil<OutInfo>> list(OutVO query) throws Exception {
        PageUtil<OutInfo> pageUtil=outInfoService.findPage(query);
        return  Result.buildResult(Result.Status.OK,pageUtil);
    }
}
